local GM = GM or gmod.GetGamemode()

GM.help = {
	overlay = true, //So people ask "ho wdoes i rmeove help/" instead of "omgh owdo i spawn" or "wtfwhy i die" or "whats th espinign cirlce for/?"
	action = false, //Although we will probably see "wtfwhy i die" anyway
}
local help = GM.help

function GM:ShowHelp()
	//Msg("HELP!\n")
	help.overlay = not help.overlay
end

function GM:ShowTeam()
	//Msg("TEAM!\n")
	help.action = true
end

hook.Add("HUDPaint","wsHelp",function()
	local self = gmod.GetGamemode()
	local help = self.help
	
	if not help.overlay then
		return
	end
	
	if self:IsRoundStarted() and not LocalPlayer():WSIsPigeon() and LocalPlayer():Alive() then //Health Circle help
		local xpos = ScrW()*.5
		local ypos = ScrH()-64
		local rot = UnPredictedCurTime()*.1%1*360
		local x,y
		
		for k,v in pairs{
			{
				label = "Cold",
				color = {255,0,0},
			},
			{
				label = "Thirst",
				color = {0,0,255},
			},
			{
				label = "Hunger",
				color = {0,255,0},
			},
			{
				label = "Fatigue",
				color = {255,255,0},
			},
		} do
			x = xpos + math.cos(math.Deg2Rad(rot*-1+45+90*(k+2)))*128
			y = ypos + math.sin(math.Deg2Rad(rot*-1+45+90*(k+2)))*128
			
			surface.SetFont("Trebuchet24")
			local w,h = surface.GetTextSize(v.label)
			draw.RoundedBox(4,x-w*.5-4,y,w+8,24,Color(0,0,0,127))
			
			draw.DrawText(v.label,"Trebuchet24",x,y,Color(unpack(v.color)),1)
		end
	end
end)

GM.help.cache_HC_ElementDesc = {
	w = 0,
	h = 0,
}

hook.Add("HUDPaint","wsHelp1",function()
//hook.Add("PostRenderVGUI","wsHelp1",function()
	local self = gmod.GetGamemode()
	local help = self.help
	
	if not help.overlay then
		return
	end
	
	local invopen = type(self.inventory.menuInventory) == "table"
	
	local xpos = 16
	local ypos = 16
	if self:IsRoundStarted() and invopen then
		local menu = self.inventory.menuInventory.main
		if menu then
			if table.Count(self.lootventory.menuInventory) == 0 then //The inventory is open
				local dat = menu:GetTable()
				local gx,gy = menu:GetPos()
				local gw,gh = menu:GetSize()
				gx = gx+gw
				
				if dat.tab == 1 then //Items
					draw.RoundedBox(8,gx,gy,256,392,Color(0,0,0,127))
					
					gx = gx+4
					gy = gy+4
					
					draw.DrawText("Inventory","Trebuchet24",gx+4,gy,Color(255,255,255,255),0)
					gy = gy+24
					
					draw.DrawText("Controls\nWhile hovering over an item:\n\t[Q] Quick-Equip\n\t[E] Consume\n\t[0-9] Equip\n\t[Click] Drag item\nWhile open:\n\t[C] Close\n\t[V] Close","Trebuchet18",gx+4,gy,Color(255,255,255,255),0)
					gy = gy + 18*9
					
					draw.DrawText("While dragging:\n\t[Scroll] Adjust quantity\n\tMove to a slot to equip\n\tMove to the right to drop\n\tMove to a bottom slot for combining\nCombining items:","Trebuchet18",gx+4,gy,Color(255,255,255,255),0)
					gy = gy + 18*6
					
					surface.SetFont("Trebuchet18")
					local disp = help.wordwrap("       To make new items, like a campfire or an axe, click and drag items to a slot in the bottom of the inventory. Once you have all the desired items, click Combine.",256-16)
					draw.DrawText(disp,"Trebuchet18",gx+4,gy,Color(255,255,255,255),0)
				else //Recipes
					draw.RoundedBox(8,gx,gy,256,392,Color(0,0,0,127))
					
					gx = gx+4
					gy = gy+4
					
					draw.DrawText("Recipes","Trebuchet24",gx+4,gy,Color(255,255,255,255),0)
					gy = gy+24
					
				surface.SetFont("Trebuchet18")
					local disp = help.wordwrap("Click on a recipe to create an item. You will need to have all the required ingredients and tools to make an item. Items in which you have in insufficient quantities are shown in red. If you do not currently have the item you are attempting to make, it will be shown in green.",256-16)
					draw.DrawText(disp,"Trebuchet18",gx+4,gy,Color(255,255,255,255),0)
					gy = gy + 18*8
					
					gy = gy + 18
					
					if table.Count(self.recipes) == 0 then
						local disp = help.wordwrap("It looks like you don't have any recipes! Try making a campfire from [wood], [rock], and [crystal].",256-16)
						draw.DrawText(disp,"Trebuchet18",gx+4,gy,Color(255,255,255,255),0)
					elseif table.Count(self.recipes) == 1 then
						local disp = help.wordwrap("You can make an axe by combining [wood], [sap], and [rock]. To get sap, equip a rock and hit a tree.",256-16)
						draw.DrawText(disp,"Trebuchet18",gx+4,gy,Color(255,255,255,255),0)
					elseif table.Count(self.recipes) == 2 then
						local disp = help.wordwrap("You can make a bow by combining [vine], [plank], and [sap].",256-16)
						draw.DrawText(disp,"Trebuchet18",gx+4,gy,Color(255,255,255,255),0)
					elseif table.Count(self.recipes) == 3 then
						local disp = help.wordwrap("You can make a plank by combining [wood], and [sap].",256-16)
						draw.DrawText(disp,"Trebuchet18",gx+4,gy,Color(255,255,255,255),0)
					elseif table.Count(self.recipes) == 4 then
						local disp = help.wordwrap("You can make a barrel by combining [plank], and [sap].",256-16)
						draw.DrawText(disp,"Trebuchet18",gx+4,gy,Color(255,255,255,255),0)
					elseif table.Count(self.recipes) >= 5 then
						local disp = help.wordwrap("You have quite a collection of recipes, here.",256-16)
						draw.DrawText(disp,"Trebuchet18",gx+4,gy,Color(255,255,255,255),0)
					end
				end
			else //We have a loot screen open
			end
		end
	elseif self:IsRoundStarted() and (not LocalPlayer():WSIsPigeon()) and not invopen then //Health Circle help details
		local gx,gy = xpos,ypos
		
		if LocalPlayer():Alive() then
			do
				local dat = self.help.cache_HC_ElementDesc
				draw.RoundedBox(4,gx-4,gy-4,dat.w+8,dat.h+8,Color(0,0,0,191))
				dat.w,dat.h = 0,0
			end
			
			local pulse = math.abs(math.sin(UnPredictedCurTime()*.4%1*2*math.pi))*255
			
			for k,v in pairs{
				{
					label = "Cold",
					color = {255,0,0},
					description = "Warm up by staying near a campfire. Build a campfire by combining [rock], [wood], and [crystal].",
				},
				{
					label = "Thirst",
					color = {0,0,255},
					description = "Drink water from a body of water by pressing [+USE]",
				},
				{
					label = "Hunger",
					color = {0,255,0},
					description = "Hunt animals for meat. While in the inventory, press [E] while hovering over the meat to consume it.",
				},
				{
					label = "Fatigue",
					color = {255,255,0},
					description = "Stay well rested. Try not to run, jump. or fall too much. Moving wears you out.",
				},
				{
					label = "Constitution",
					color = {pulse,pulse,pulse},
					description = "The white halo around all the other needs. Neglecting even one need will darken the halo. If the halo turns solid black, you will soon die. Taking damage from falls and cannibals will also deteriorate your constitution.",
				},
			} do
				//Word wrap
				surface.SetFont("Trebuchet18")
				
				local dout = help.wordwrap(v.description,256)
				
				draw.DrawText(v.label,"Trebuchet24",gx,gy,Color(unpack(v.color)),0)
				gy = gy + 24
				
				draw.DrawText(dout or "","Trebuchet18",gx,gy,Color(255,255,255,255),0)
				local w,h = surface.GetTextSize(dout)
				local dat = self.help.cache_HC_ElementDesc
				dat.w = math.max(dat.w,w)
				dat.h = dat.h+h+24
				gy = gy + h
			end
		else
			draw.RoundedBox(4,gx-4,gy-4,256+8,24+18*3+8,Color(0,0,0,191))
			draw.DrawText("You're dead!","Trebuchet24",gx,gy,Color(255,255,255,255),0)
			draw.DrawText("Death is the number one cause of dead\npeople in Winter. Try not to die next time.\nTo disable help, press [F1].","Trebuchet18",gx,gy+24,Color(255,255,255,255),0)
		end
	elseif LocalPlayer():WSIsPigeon() then
		local gx,gy = xpos,ypos + (self:IsRoundStarted() and 0 or 56)
		surface.SetFont("Trebuchet18")
		local disp
		
		if self:IsRoundStarted() then
			disp = "You are spectating as a crow. You will respawn when a new round starts."
		else
			local time = math.floor(self.RoundTime and self.RoundTime-CurTime() or 0)
			local s = true
			if time == 1 then
				s = false
			end
			disp = "The next round will start in "..tostring(time).." second"..(s and "s" or "")..". For the time being, you are spectating as a crow. The game may lag while the game items are spawning."
		end
		
		surface.SetFont("Trebuchet18")
		disp = help.wordwrap(disp,256)
		//Wordwrap does not recognize new lines yet, so...
		disp = disp.."\n\nBird Controls\nGeneral:\n\t[Attack] Caw Alert\n\t[Attack2] Caw Calm\nIn Air:\n\t[Forward] Fly forward\n\t[Jump] Ascend\nOn Ground:\n\t[Move] Run\n\t[Walk+Move] Walk\n\t[Jump] Takeoff\n\t[None] Peck ground"
		disp = disp..help.wordwrap("\n\nNote:\nPecking the ground is offensive to some cannibals. Be weary.",256)
		
		local w,h = surface.GetTextSize(disp)
		
		draw.RoundedBox(4,gx-4,gy-4,w+8,h+8,Color(0,0,0,100))
		draw.DrawText(disp or "","Trebuchet18",gx,gy,Color(255,255,255,255),0)
	end
end)

help.wordwrap = function(str,width)
	local dp = string.Explode(" ",str)
	local dout = dp[1]
	local curline = dout
	for k,v in pairs(dp) do
		if k ~= 1 then
			local sz = surface.GetTextSize(curline)
			local sza = surface.GetTextSize(" "..v)
			if sz+sza < width then
				dout = dout .. " " .. v
				curline = curline .. " " .. v
			else
				dout = dout .. "\n" .. v
				curline = v
			end
		end
	end
	
	return dout
end































